﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using IronOne.SalesOne.BusinessObjects;
using Facade;
using System.Collections.Generic;


namespace IronOne.SalesOne.SalesOneWeb.UserControls
{
    public partial class UCSearch : System.Web.UI.UserControl
    {
        private String action = "Manage Branch";
        //private String action="Manage Unit";
        String drpDownVal;
        List<SalesOneUnit> grp;
        int branchIndex;
        //public int loadNumber=1;
        //public List<Branch> br;

        //String grpName;

        /*
        public String getSelectedValue()
        {
            return grpName;  
        }
         * 
         * */

        protected void Page_Load(object sender, EventArgs e)
        {
           
            
            if (action == "Manage group Users")
            {
                lblHeader.Text = "Manage group Users";
                lblBranch.Visible = false;
                lblUnit.Visible = false;
                drpdownBranch.Visible = false;
                drpdownUnit.Visible = false;
            }

            else if (action == "Manage Branch")
            {
                
                lblHeader.Text = "Manage Branch";
                lblBranch.Visible = false;
                lblUnit.Visible = false;
                drpdownBranch.Visible = false;
                drpdownUnit.Visible = false;
                vldBranch.Enabled = false;
                vldUnit.Enabled = false;
                Label4.Visible = false;
                Label5.Visible = false;
                if (!IsPostBack)
                {
                    FillDropDown("Group");
                }
            }

            else if (action == "Manage Branch Users")
            {
                lblHeader.Text = "Manage Branch Users";
                lblUnit.Visible = false;
                drpdownUnit.Visible = false;
            }
            else if (action == "Manage Unit")
            {
                lblHeader.Text = "Manage Unit";
                
                lblUnit.Visible = false;
                drpdownUnit.Visible = false;
                vldUnit.Enabled = false;
                Label5.Visible = false;

                if (!IsPostBack)
                {
                    FillDropDown("Group");
                }
                    
                
            }
            else if (action == "Manage Unit Users")
            {
                lblHeader.Text = "Manage Unit Users";
            }


        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //SearchWeb sw = new SearchWeb();
            //sw.GName = drpdownGroup.SelectedItem.ToString();
            //sw.BName = drpdownBranch.SelectedItem.ToString();
            //sw.UName = drpdownUnit.SelectedItem.ToString();

            if (lblHeader.Text == "Manage Branch")
            {
               // string gp = drpdownGroup.SelectedItem.ToString();
                StructureFacade sf = new StructureFacade();
                UCUserGrid ug = new UCUserGrid();
                ug.SetDataSource(grp);

                ManageBranches mb = new ManageBranches();
              //  mb.DataSource123(gp);
                
                //br=sf.GetBranchesFromGroup(gp);
                
             
            }

            else if (lblHeader.Text == "Manage group Users")
            {
            }
            else if (lblHeader.Text == "Manage Branch Users")
            {
            }
            else if (lblHeader.Text == "Manage Unit")
            {
                
            }


        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
 
            if (action == "Manage Branch")
            {
                vldBranch.Enabled = false;
                vldUnit.Enabled = false;
                vldGroup.Enabled = false;
                string pageUrl = Request.ServerVariables["URL"];
                if (pageUrl == "/Structure/ManageBranches.aspx")
                {
                    Session["BranchAc"] = "Create";
                    Response.Redirect("/Structure/AddBranch.aspx");

                    
                }
            }
            else if (action == "Manage Unit")
            {
               // vldBranch.Enabled = false;
               // vldUnit.Enabled = false;
               // vldGroup.Enabled = false;
               //vldBranch.Visible = false;

                string pageUrl = Request.ServerVariables["URL"];
                if (pageUrl == "/Structure/ManageUnits.aspx")
                {
                    Session["UnitAc"] = "Create";
                    Response.Redirect("/Structure/AddUnit.aspx");


                }
                
            }

        }
        public void FillDropDown(string type)
        {
            StructureFacade fac = new StructureFacade();

            switch (type)
            {
                case "Group":
                    List<SalesOneGroup> grp = fac.GetGroups("GroupId ASC");
                    SalesOneGroup grsel = new SalesOneGroup();
                    grsel.GroupId = -1;
                    grsel.Name = "Select";
                    grp.Insert(0, grsel);
                    drpdownGroup.DataSource = grp;
                    drpdownGroup.DataTextField = "Name";
                    drpdownGroup.DataValueField = "GroupId";
                    drpdownGroup.DataBind();
                    
                    break;

                case "Branch":
                    if (drpdownGroup.SelectedValue != "-1")
                    {
                        List<Branch> br = fac.GetBranchesForGroup(Convert.ToInt32(drpdownGroup.SelectedValue));
                        drpdownBranch.DataSource = br;
                        Branch brSel = new Branch();
                        brSel.BranchId = -1;
                        brSel.BranchName = "Select";
                        br.Insert(0, brSel);
                        drpdownBranch.DataTextField = "BranchName";
                        drpdownBranch.DataValueField = "BranchId";
                        drpdownBranch.DataBind();
                    }
                    break;

                default:
                    break;
            }
        }

        protected void drpdownGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lblHeader.Text == "Manage Unit")
            {
                FillDropDown("Branch");
            }
            else if (lblHeader.Text == "Manage Branch")
            {
                StructureFacade fc = new StructureFacade();
                
                int gruopIndex = (Convert.ToInt32(drpdownGroup.SelectedValue));
                List<Branch> br=fc.GetBranchesForGroup(gruopIndex);
                UCUserGrid ug = new UCUserGrid();
                ug.SetDataSource(br);
                
                
            }

        }

        protected void drpdownBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lblHeader.Text == "Manage Unit")
            {
                branchIndex = (Convert.ToInt32(drpdownBranch.SelectedValue));
                StructureFacade sf = new StructureFacade();
                List<SalesOneUnit> unitList = sf.GetUnitsForBranch(branchIndex);
                UCUserGrid ug = new UCUserGrid();
                ug.SetDataSource(unitList);
            }
            
        }

    }
}